#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Sanchez
#
# Created:     01/06/2012
# Copyright:   (c) Sanchez 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------

from moddt8837 import *
import pylab as plt
import time
import numpy as np
import scipy as sc
import struct
import time

DEV1 = "TCPIP::169.254.241.13::INSTR";


#-------------------------------------------------------------------------------
# reccupere le handle des boitiers
#-------------------------------------------------------------------------------
h1 = getHandle(DEV1)


#-------------------------------------------------------------------------------
# lecture du nom des boitires, identification
#-------------------------------------------------------------------------------
s1=getID(h1)

print s1
#-------------------------------------------------------------------------------
# Enable AD for all channel
#-------------------------------------------------------------------------------
print "Activation des  modules AD..."
err1 = setAdEnableAll(h1)
#-------------------------------------------------------------------------------
# couplage AC/DC pour tous les modules
#-------------------------------------------------------------------------------
print "Couplage  des  voies en AC ..."
err1 = setCouplage(h1,'AC')
#-------------------------------------------------------------------------------
# initialisation des gains pour toutes les voies  1/10
#-------------------------------------------------------------------------------
print "Initialisation des gains ..."
level=1   # 1V en entree 1 ou 10
err1 = setGain(h1,level)
#-------------------------------------------------------------------------------
# mode acquisition du buffer wrap/no wrap
#-------------------------------------------------------------------------------
print "Mode acquisition buffer wrap/no wrap ..."
err1 = setWrap(h1,"on")  #   buffer circulaire
h1.write("ad:buffer:mode?")
print h1.read()
h1.write("ad:buffer:size?")
print h1.read()
h1.write("ad:buffer:size:sca?")
print h1.read()
#-------------------------------------------------------------------------------
# horloge interne
#-------------------------------------------------------------------------------
print "Initialisation horloge interne ..."
err1=setADClockInternal(h1)
#-------------------------------------------------------------------------------
# frequence horloge interne
#-------------------------------------------------------------------------------
print "Initialisation frequence horloge interne ..."
fs= 10000 # Hz
err1=setFreqClock(h1,fs)
dt=1.0/fs    #periode echantillonnange
#-------------------------------------------------------------------------------
# trigger immediat / software
#-------------------------------------------------------------------------------
print "Initialisation trigger source immediat ..."
err1=setAdTrigSourceImm(h1)
#-------------------------------------------------------------------------------
# pret a lancer la mesure
#-------------------------------------------------------------------------------
print "Armement des mesures, pret a lancer ..."
err1=setAdArm(h1)
status=0
while (status>0):
    # status
    err1=h1.write("AD:STATus?")
    status=int(h1.read())
    print status
#-------------------------------------------------------------------------------
# lance la mesure la mesure
#-------------------------------------------------------------------------------
print "lance la mesure ...."
err1=setAdInit(h1)
tacq=10# temps acquisition 1s
##print "attente ..."
buffersize=tacq/dt # nombre d echantillon pour tacq
##print "nb. element : ", buffersize

N=254 #254  # work well only with 100 !!!!!!!!!!!!!!!!! or less than 255 (254)


nbblock=int(buffersize/N-1)
##print "nb. de blocs : ",nbblock
time.sleep(tacq)  #attente 10s

#-------------------------------------------------------------------------------
# arrete la mesure la mesure
#-------------------------------------------------------------------------------
err1=setAdStop(h1)
#err2=setAdStop(h2)


#-------------------------------------------------------------------------------
# on reccupere les donnees
#-------------------------------------------------------------------------------
t0= time.clock()

mess=":AD:FETCH? 0,"+str(N-1)+"\n"
##print "---------------------------"
##print mess
err1=h1.write (mess)
data=h1.read_raw()



sizeHeader,sizeBuffer,header,buf =  readHeader(data)
##print sizeHeader,sizeBuffer
##print header

for i in range(1,nbblock):
##    print "---------------------------"
    mess=":AD:FETCH? "+str(i*N) +","+str(N)+"\n"
##    print mess
    err1=h1.write (mess)
##    print "retour fetch = ",err1
    data1=h1.read_raw()
    sizeHeader,sizeBuffer,header,buf1 =  readHeader(data1)
##    print sizeHeader,sizeBuffer
##    print header
    #
    buf=np.concatenate(( buf,buf1), axis=0)

t= time.clock() - t0 # t is wall seconds elapsed (floating point)
print "duree = ",t

print "arret de la mesure ...."
plt.figure(1)
plt.plot(buf)
##fic = open("sinus.dat","wb")
##fic.write(data)
##fic.close()
plt.show()
exit(0)
